package edu.whut.mall.portal.controller;

import edu.whut.mall.common.api.CommonResult;
import edu.whut.mall.portal.domain.PayInfoParams;
import edu.whut.mall.portal.service.IPayInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.security.NoSuchAlgorithmException;

@Slf4j
@RequiredArgsConstructor
@RequestMapping("/payInfo")
@RestController
@Api(value="支付信息管理器",tags = {"支付信息管理器"})
public class RestPayInfoController {

    private final IPayInfoService payInfoService;

    @ApiOperation("团购用户支付")
    @PostMapping(value = "/pay")
    public CommonResult create(@Valid @RequestBody PayInfoParams payInfoParams) throws NoSuchAlgorithmException {
           return payInfoService.Create(payInfoParams);
    }

    @ApiOperation("异步通知")
    @PostMapping(value = "/notify")
    public String asyncNotify(HttpServletRequest request) throws NoSuchAlgorithmException {
        return payInfoService.asyncNotify(request);
    }

    @ApiOperation("申请退款")
    @PostMapping(value = "/requestFund")
    public CommonResult requestFund(String orderNo, String reason) {
        int result = payInfoService.requestFund(orderNo, reason);
        if (result < 1) {
            return CommonResult.failed("申请退款失败");
        }
        return CommonResult.success("申请退款成功");
    }


}